//
//  VKVoucherChildCell.m
//  V-Learn
//
//  Created by liubo on 2019/1/10.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKVoucherChildWeiCell.h"

@implementation VKVoucherChildWeiCell{
    UIImageView *_bgImageV;
    UILabel *_titleLabel;
    UILabel *_timeLabel;
    UILabel *_moneyLabel;

}

+ (instancetype)cellWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath withModel:(VKVoucherChildModel *)model{
    VKVoucherChildWeiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VKVoucherChildWeiCell"];
    if (cell == nil) {
        cell = [[VKVoucherChildWeiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"VKVoucherChildWeiCell"];
    }
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    cell.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    
    return cell;
}
+ (void)downCountAttributedleftString:(NSString *)leftString withRightString:(NSString *)rightString withLabel:(UILabel *)contentLabel {
    
    // 左边的文字颜色
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:leftString attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17.0]}];
    // 右边的文字颜色
    NSMutableAttributedString *rightS = [[NSMutableAttributedString alloc]initWithString:rightString attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:25.0]}];
    [string insertAttributedString:rightS atIndex:string.length];
    contentLabel.attributedText = string;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 点击cell的时候不要变色
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // 布局子控件
        [self autoLayout];
    }
    return self;
}

-(void)setModel:(VKVoucherChildModel *)model{
    _model = model;
    _titleLabel.text = model.voucherName;
    [VKVoucherChildWeiCell downCountAttributedleftString:@"¥" withRightString:model.voucherPrice withLabel:_moneyLabel];

    NSString *start = [model.voucherStartTime substringToIndex:10];
    NSString *end = [model.voucherEndTime substringToIndex:10];
    _timeLabel.text = [NSString stringWithFormat:@"有效期:%@-%@",[start stringByReplacingOccurrencesOfString:@"-" withString:@"."],[end stringByReplacingOccurrencesOfString:@"-" withString:@"."]];

    if (model.isSelectRow == NO) {
        [_deleteBtn setImage:[UIImage imageNamed:@"meo"] forState:UIControlStateNormal];
    }else if (model.isSelectRow == YES){
        [_deleteBtn setImage:[UIImage imageNamed:@"meselect"] forState:UIControlStateNormal];
    }
    
    if (model.row == 0) {
        [_bgImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@15);
        }];
    }
    if (model.isSelect == NO) { // 隐藏
        _deleteBtn.hidden = YES;
        [_bgImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
//            make.right.equalTo(@(0));
            make.width.equalTo(@([UIScreen cz_screenWidth]));
        }];
        [_lijiBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_bgImageV.mas_right).offset(-35);
        }];
    }else if (model.isSelect == YES){// 显示
        _deleteBtn.hidden = NO;
        [_bgImageV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@65);
//            make.right.equalTo(@(15));
            make.width.equalTo(@([UIScreen cz_screenWidth]));
        }];
        
        [_lijiBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_bgImageV.mas_right).offset(-20);
        }];
    }
}

- (void)autoLayout{
    
    UIImageView *bgImageV = [[UIImageView alloc]init];
    _bgImageV = bgImageV;
    [self.contentView addSubview:bgImageV];
    bgImageV.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    bgImageV.image = [UIImage imageNamed:@"Coupon"];
    [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
//        make.right.equalTo(@(0));
        make.top.mas_equalTo(@0);
        make.bottom.equalTo(self.contentView);
        make.width.equalTo(@([UIScreen cz_screenWidth]));
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn = button;
    button.hidden = YES;
    [self.contentView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@35);
        make.left.equalTo(@20);
        make.centerY.equalTo(self.contentView);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:titleLabel];
    _titleLabel = titleLabel;
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgImageV).offset(30);
        make.top.mas_equalTo(bgImageV).offset(20);
    }];
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    _moneyLabel = moneyLabel;
    [self.contentView addSubview:moneyLabel];
    moneyLabel.textColor = [UIColor whiteColor];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgImageV).offset(-50);
        make.centerY.mas_equalTo(titleLabel);
    }];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    _timeLabel = timeLabel;
    
    [self.contentView addSubview:timeLabel];
    timeLabel.textColor = [UIColor cz_colorWithRed:177 green:177 blue:177];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgImageV).offset(30);
        make.right.mas_equalTo(-30);
        make.bottom.mas_equalTo(bgImageV).offset(-40);
    }];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    
//    UIButton *lijiBtn = [[UIButton alloc]init];
//    _lijiBtn = lijiBtn;
//    [self.contentView addSubview:lijiBtn];
//    [lijiBtn setTitleColor:[UIColor cz_colorWithRed:255 green:124 blue:69] forState:UIControlStateNormal];
//    lijiBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//    [lijiBtn setTitle:@"立即使用" forState:UIControlStateNormal];
//    lijiBtn.layer.cornerRadius = 15;
//    lijiBtn.layer.masksToBounds = YES;
//    lijiBtn.layer.borderWidth = 1;
//    lijiBtn.layer.borderColor = [UIColor cz_colorWithRed:255 green:124 blue:69].CGColor;
//    [lijiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(bgImageV.mas_right).offset(-35);
//        make.centerY.mas_equalTo(timeLabel);
//        make.width.equalTo(@80);
//        make.height.equalTo(@30);
//    }];
}
@end

